class Book(val title: String, val author: String, val ISBN: String, var isBorrowed: Boolean = false) {
  def borrow(): Unit = {
    if (!isBorrowed) {
      isBorrowed = true
      println(s"$title by $author with ISBN $ISBN has been borrowed.")
    } else {
      println(s"$title by $author with ISBN $ISBN is already borrowed.")
    }
  }

  def returnBook(): Unit = {
    if (isBorrowed) {
      isBorrowed = false
      println(s"$title by $author with ISBN $ISBN has been returned.")
    } else {
      println(s"$title by $author with ISBN $ISBN is already returned.")
    }
  }
}

object Book {
  def apply(title: String, author: String, ISBN: String): Book = new Book(title, author, ISBN)

  def unapply(book: Book): Option[(String, String, String, Boolean)] =
    Some((book.title, book.author, book.ISBN, book.isBorrowed))
}

class Library {
  private var availableBooks: List[Book] = Nil
  private var borrowedBooks: List[Book] = Nil

  def addBook(book: Book): Unit = {
    availableBooks ::= book
    println(s"$book has been added to the library.")
  }

  def borrowBook(title: String, author: String, ISBN: String): Unit = {
    availableBooks.find(b => b.title == title && b.author == author && b.ISBN == ISBN) match {
      case Some(book) =>
        availableBooks = availableBooks.filter(b => b != book)
        borrowedBooks ::= book
        book.borrow()
      case None => println(s"No such book found: $title by $author with ISBN $ISBN.")
    }
  }

  def returnBook(title: String, author: String, ISBN: String): Unit = {
    borrowedBooks.find(b => b.title == title && b.author == author && b.ISBN == ISBN) match {
      case Some(book) =>
        borrowedBooks = borrowedBooks.filter(b => b != book)
        availableBooks ::= book
        book.returnBook()
      case None => println(s"No such book borrowed: $title by $author with ISBN $ISBN.")
    }
  }

  def showAvailableBooks(): Unit = {
    if (availableBooks.isEmpty) {
      println("No available books.")
    } else {
      availableBooks.foreach(book => println(s"${book.title} by ${book.author}, ISBN: ${book.ISBN}"))
    }
  }

  def showBorrowedBooks(): Unit = {
    if (borrowedBooks.isEmpty) {
      println("No borrowed books.")
    } else {
      borrowedBooks.foreach(book => println(s"${book.title} by ${book.author}, ISBN: ${book.ISBN}"))
    }
  }
}

//创建新书
val book1 = Book("算法导论", "Thomas H. Cormen", "9780262033848")
val book2 = Book("人月神话", "Frederick P. Brooks Jr.", "9787111128069")

//创建图书馆
val library = new Library

//将书添加到图书馆中
library.addBook(book1)
library.addBook(book2)

//显示当前可用的书籍
library.showAvailableBooks()

//借出一本书
library.borrowBook("算法导论", "Thomas H. Cormen", "9780262033848") 

//再次查看可用的书籍和已借出的书籍
library.showAvailableBooks()
library.showBorrowedBooks()

//尝试借出已被借出的书籍或归还未被借出的书籍
library.borrowBook("算法导论", "Thomas H. Cormen", "9780262033848")
library.returnBook("人月神话", "Frederick P. Brooks Jr.", "9787111128069")
